package com.qingke.oms.command.user;


import com.qingke.oms.command.UserCommand;
import com.qingke.oms.pojo.Goods;
import com.qingke.oms.pojo.GoodsType;
import com.qingke.oms.pojo.Manufacturer;
import com.qingke.oms.pojo.User;
import com.qingke.oms.util.OMSConsole;

public class ApplyNewGoodsCommand extends UserCommand {

	public ApplyNewGoodsCommand(User user) {
		super(user);
	}

	@Override
	public void execute() {
		String code;
		String name;
		String goodsTypeCode;
		String manutacurerCode;
		String description;
		while(true){
			code=OMSConsole.promptNotEmpty("code:");
			if(dao.isGoodsExistByCode(code)){
				OMSConsole.println("The code have been existed.");
				continue;
			}
			break;
		}
		name=OMSConsole.promptNotEmpty("name:");
		while(true){
			goodsTypeCode=OMSConsole.promptNotEmpty("goods type code:");
			if(!dao.isGoodsTypeExistByCode(goodsTypeCode)){
				OMSConsole.println("The goods type is not existed.");
				continue;
			}
			break;
		}
		while(true){
			manutacurerCode=OMSConsole.promptNotEmpty("manufacturer code:");
			if(!dao.isManufacturerExistByCode(manutacurerCode)){
				OMSConsole.println("The manufacturer is not existed.");
				continue;
			}
			break;
		}
		description=OMSConsole.promptNotEmpty("description:");
		
		GoodsType gt=dao.selectGoodsTypeByCode(goodsTypeCode);
		Manufacturer m=dao.selectManufacturerByCode(manutacurerCode);
		Goods goods=new Goods();
		goods.setCode(code);
		goods.setName(name);
		goods.setGoodsType(gt);
		goods.setManufacturer(m);
		goods.setDescription(description);
		boolean isSuccess=dao.insertGoods(goods);
		if(isSuccess)
			OMSConsole.println("new a goods success.");
		else
			OMSConsole.println("new a goods failed.");
	}

}
